package com.dq.health.utils;

import java.security.SecureRandom;
import java.util.Random;

public final class RandomCodeUtil {
    private static final String SYMBOLS = "0123456789"; // 数字
    private static final String SYMBOLS_LETTLE = "0123456789abcdefghijklmnopqrstuvwxyz"; // 数字+字母
    private static final Random RANDOM = new SecureRandom();

    public static String generateVerifyCode(int length) {
        if (length < 1 || length > 30) {
            length = 6;
        }

        char[] codeChars = new char[length];

        for (int index = 0; index < codeChars.length; ++index) {
            codeChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }

        return new String(codeChars);
    }

    public static String generateInitPassword(int length) {
        if (length < 1 || length > 30) {
            length = 6;
        }

        char[] codeChars = new char[length];

        for (int index = 0; index < codeChars.length; ++index) {
            codeChars[index] = SYMBOLS_LETTLE.charAt(RANDOM.nextInt(SYMBOLS_LETTLE.length()));
        }

        return new String(codeChars);
    }
}
